<?php

namespace frontend\controllers;

use Yii;
use common\models\Zhongyao;
use yii\data\Pagination;
use common\components\CatelogHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ZhongyaoController implements the CRUD actions for Zhongyao model.
 */
class ZhongyaoController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            // index页面缓存，1天有效
            'indexCache' => [
                'class' => 'common\components\PageCache',
                'only' => ['index'],
                'duration' => 0,
                'variations' => [\Yii::$app->language,],
                'dependency' => [
                    'class' => 'yii\caching\DbDependency',
                    'sql' => 'SELECT `updated_at` FROM `zhongyao` ORDER BY `id` DESC LIMIT 1',
                ],
            ],            
            // view页面缓存，1天有效
            'viewCache' => [
                'class' => 'common\components\PageCache',
                'only' => ['view'],
                'duration' => 86400,
                'variations' => [\Yii::$app->language,],
                'dependency' => [
                    'class' => 'yii\caching\DbDependency',
                    'sql' => 'SELECT `updated_at` FROM `zhongyao` ORDER BY `id` DESC LIMIT 1',
                ],
            ],
            // Http缓存
            'HttpCache' => [
                'class' => 'yii\filters\HttpCache',
                'only' => ['view'],
                'etagSeed' => function ($action, $params) {
                    $post = Zhongyao::find()->select(['id'])->orderBy('id desc')->limit(1)->asArray()->one();
                    return serialize([$post]);
                },
            ],
        ];
    }

    /**
     * Lists all Zhongyao models.
     * @return mixed
     */
    public function actionIndex()
    {
        $query = Zhongyao::find()->asArray()->where(['enable'=>1]);
        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $query->count(),
        ]);
        $model = $query->orderBy('id desc')
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();
        return $this->render('index', [
                    'model' => $model,
                    'pagination' => $pagination,
        ]);
    }

    /**
     * Displays a single Zhongyao model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = Zhongyao::find()->where(['id' => $id])->andWhere(['enable'=>1])->asArray()->one();
        if (!empty($model)){
            $next = Zhongyao::find()->where(['>', 'id', $model['id']])->andWhere(['enable'=>1])
                            ->limit(1)->asArray()->one();
            $last = Zhongyao::find()->where(['<', 'id', $model['id']])->andWhere(['enable'=>1])
                            ->orderBy(['id' => SORT_DESC])->limit(1)->asArray()->one();
            $relate = Zhongyao::find()->select(['id','title'])->where(['type_id'=>$model['type_id']])
                    ->andWhere(['!=', 'id', $model['id']])->andWhere(['enable'=>1])
                    ->OrderBy('view desc')->limit(20)->asArray()->all();
            $tag = \common\components\FrontendHelper::Sltopic_relate($model['type_id'],15);
            // 更新浏览量
            CatelogHelper::updateViews('zhongyao', $model['view'], $id);
            return $this->render('view', [
                        'model' => $model,
                        'last' => $last,
                        'next' => $next,
                        'relate' => $relate,
                        'tag' => $tag,
            ]);
        }  else {
            throw new NotFoundHttpException(\Yii::t('common', 'NotFound'));
        }
    }
}
